﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DomainModel;
using Utils;
using WebUI.Utils;

namespace WebUI.Models
{
    public class JobPostingViewModel : BaseViewModel
    {
        public JobPostingViewModel()
            : base()
        { }
        //3 steps: first step has job info, company info and company avatar which in tempimage
        //the second step just preview, keep the data
        //the third step contains sensitive data, if user enter something, and then go back and forth, the user will lose the data in the third step
        //it means we don't store data in the third step even temporarily

        public JobPostingViewModel(string jobToken)
            : base(new { JobToken = jobToken })
        { }

        public Job Job { get; set; }
        public string JobToken { get; set; }

        public Co Co { get; set; }
        public HttpPostedFileBase Avatar { get; set; }
        public string AvatarUrl { get; set; }

        public IEnumerable<SelectListItem> JobExpList { get; set; }
        public IEnumerable<SelectListItem> JobTypeList { get; set; }
        public IEnumerable<SelectListItem> CoTypeList { get; set; }

        protected override void SetVarsWhichDependOnDynamicParams(dynamic dynParams)
        {
            string jobToken = dynParams.JobToken;
            JobToken = jobToken;
            //if not co logged in, we always get jobToken from cookie, and the form url doesn't contains jobToken, so why we set empty to JobToken
            if (!Identity.IsCoLoggedIn)
            {
                JobToken = string.Empty;
            }
            //JobToken in cooke contains JobGUID-JobID pair
            string jobGUID = ViewModelUtils.GetJobGUIDFromToken(jobToken);
            int jobID = ViewModelUtils.GetJobIDFromToken(jobToken);
            //companyID
            int coID = Identity.IsCoLoggedIn ? Identity.AccountID : 0;
            
            //for any case, just call this to get CoJob
            CoJob coJob = DataUtils.GetCoJobByJobTokenAndCoID(jobGUID, jobID, coID);
            Job = coJob.Job;
            Co = coJob.Co;

            AvatarUrl = ImageUtils.GetAvatarRelativeUrl(AvatarType.CoAvatar, Co.CoID, AvatarSizeEnum.Avatar_80x80, Co.AvatarFileExtension);
        }

        public override void SetPublicVarsAfterBindingDynamicParams()
        {
            JobExpList = ViewModelUtils.GetSelectListItems(GlobalUtilities.GetInstance().JobExpList, Job.JobExpID);
            JobTypeList = ViewModelUtils.GetSelectListItems(GlobalUtilities.GetInstance().JobTypeList, Job.JobTypeID);
            CoTypeList = ViewModelUtils.GetSelectListItems(GlobalUtilities.GetInstance().CoTypeList, Co.CoTypeID);
        }
    }
}